﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using LanDBom.Models;
using Microsoft.Extensions.DependencyModel;
using Newtonsoft.Json;
using LanDBom.Models.Procure;
using NPOI.OpenXmlFormats.Spreadsheet;

namespace LanDBom.Controllers
{
    public class libraysController : Controller
    {
        private LDbaseEntities db = new LDbaseEntities();
        Find_Libary Find_Libary=new Find_Libary();
        // GET: librays
        public async Task<ActionResult> Index()
        {
            var libray = db.libray.Include(l => l.Steelmaking);
            return View(await libray.ToListAsync());
        }
        // GET: librays/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            libray libray = await db.libray.FindAsync(id);
            if (libray == null)
            {
                return HttpNotFound();
            }
            return View(libray);
        }

        // GET: librays/Create
        public ActionResult Create()
        {
            ViewBag.ST_ID = new SelectList(db.Steelmaking, "ST_ID", "ST_fne");
            return View();
        }

        // POST: librays/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "LB_ID,LB_bai,LB_fne,ST_ID,LB_dan,LB_jia,LB_shun")] libray libray)
        {
            if (ModelState.IsValid)
            {
                db.libray.Add(libray);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.ST_ID = new SelectList(db.Steelmaking, "ST_ID", "ST_fne", libray.ST_ID);
            return View(libray);
        }

        // GET: librays/Edit/5
        public async Task<ActionResult> Edit(string UPid)
        {
            int id = int.Parse(UPid); 
            if (UPid == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            libray libray = await db.libray.FindAsync(id);
            if (libray == null)
            {
                return HttpNotFound();
            }
            var steelmakingItems = db.Steelmaking.ToList();
            int? selectedSteelmakingId = libray?.ST_ID;
            ViewBag.ST_ID = new SelectList(steelmakingItems, "ST_ID", "ST_fne", selectedSteelmakingId);
            return View(libray);
        }

        // POST: librays/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "LB_ID,LB_bai,LB_fne,ST_ID,LB_dan,LB_jia,LB_shun")] libray libray)
        {
            if (ModelState.IsValid)
            {
                db.Entry<libray>(libray).State = EntityState.Modified;
                int SHUNF = db.SaveChanges();
                //工艺变更保存
                if (SHUNF > 0)
                {
                    schedule schedules = new schedule()
                    {
                        MA_ID = libray.LB_ID,
                        SC_time = DateTime.Now.ToString("d"),
                        SC_Price = libray.LB_jia,
                        SC_Sun = libray.LB_shun == null ? 0 : int.Parse(libray.LB_shun)
                    };
                    db.schedule.Add(schedules);
                    int fghjk = db.SaveChanges();
                }
                return Content("<script>alert('修改成功！');window.location.href='../Material/AddGong';</script>");
            }
            ViewBag.ST_ID = new SelectList(db.Steelmaking, "ST_ID", "ST_bai", libray.ST_ID);
            return View(libray);
        }

        // GET: librays/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            libray libray = await db.libray.FindAsync(id);
            if (libray == null)
            {
                return HttpNotFound();
            }
            return View(libray);
        }

        // POST: librays/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            libray libray = await db.libray.FindAsync(id);
            db.libray.Remove(libray);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        //条件查询

    }
}
